<script setup>
import {defineProps, ref, defineEmits} from "vue";
import {ElMessage} from "element-plus";
import {insertExam, insertExamQuestionOptions, insertExamQuestions} from "../../../api/course/course.js";


// 获取关闭弹窗
const isInsertAssignment = ref(false);
const props = defineProps(['isInsertAssignment', 'practices']);
isInsertAssignment.value = props.isInsertAssignment;
// 触发父类方法
const emit = defineEmits(['close']);
// 时间单位
const timeUnit = ref(['分钟', '小时']);
// 上传考试信息
const handleInertAssignment = () => {
  console.log(props.practices);
  assignment.value.courseId = localStorage.getItem('courseId');
  console.log(assignment.value);
  // 判断作业题目和答案是否为空
  for (let i = 0; i < props.practices.length; i++) {
    for (let j = 0; j < props.practices[i].content.length; j++) {
      if (props.practices[i].content[j].content == '') {
        ElMessage.error('有题目为空,无法上传');
        return;
      }
      if (props.practices[i].content[j].answer == '') {
        ElMessage.error('有答案为空,无法上传');
        return;
      }
    }
  }
  //   上传作业信息
  const newContentArray = props.practices.flatMap(item =>
      item.content.map(({answer, content, id, type}) => ({answer, content, id, type}))
  );
  // 作业选项信息解析
  const optionsArray = props.practices.flatMap(item =>
      item.content
          .filter(obj => obj.option)  // 过滤掉没有 option 字段的对象
          .map(({option}) => option)  // 提取 option 字段
  );
  console.log(optionsArray);
  console.log(newContentArray);
  // 上传作业题目
  assignment.value.totalTime=hour.value*60+minute.value;
  console.log(assignment.value);
  insertExam(assignment.value).then(res=>{
    assignment.value.id=res.data;
    //   上传作业题目
    insertExamQuestions(assignment.value.id,newContentArray).then(res=>{
      const ids=res.data;
      console.log(ids);
      for(let i=0;i<ids.length;i++){
        optionsArray[i].assignmentQuestionId=ids[i];
      }
      console.log(optionsArray);
      insertExamQuestionOptions(optionsArray).then(res=>{
        emit('close');
        ElMessage.success('考试上传成功');
      });
    })
  })
}

// assignment对象
const assignment = ref({
  id: '',
  courseId: '',
  name: '',
  startTime: '',
  endTime: '',
  totalTime: '',
})
// 分钟
const minute = ref(0);
// 小时
const hour = ref(0);

</script>

<template>
  <el-dialog
      v-model="isInsertAssignment"
      title="填写考试信息"
      width="500"
      align-center
  >
    <el-form
        :label-position="'left'"
        label-width="auto"
        :model="assignment"
        style="max-width: 600px"
    >
      <el-form-item label="考试名称">
        <el-input v-model="assignment.name" placeholder="请输入考试名称"/>
      </el-form-item>

      <div class="demo-time-range">
        <el-form-item label="考试开始时间">
          <el-date-picker
              v-model="assignment.startTime"
              type="datetime"
              placeholder="开始时间"
          />
        </el-form-item>
        <el-form-item label="考试结束时间">
          <el-date-picker
              v-model="assignment.endTime"
              type="datetime"
              placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="考试时长">
          <div style="margin-bottom: 20px">
            <el-input-number v-model="minute" :min="1"/>
            <span style="margin-left: 20px">分钟</span>
          </div>
          <div>
            <el-input-number v-model="hour" :min="0"/>
            <span style="margin-left: 20px">小时</span>
          </div>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('close')">取消</el-button>
        <el-button type="primary" @click="handleInertAssignment">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">


</style>